﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Twi.COMMON.Core
{
    /*
     * CopyRight:©2014 踏影软件（tyingsoft）工作室版权所有
     * Author:xuzhihong
     * Create Date: 2014-11-09
     * Description: 配置的数据操作辅助类
     */
    public class ConfigDataHelper
    {
        static string twiConfigDataPath = TwiPathHelper.MapPath("~/Config/Data");

        /// <summary>
        /// 根据配置条件查找ConfigData
        /// </summary>
        /// <param name="configDataModelStr">ConfigDataModel序列化字符串</param>
        /// <returns></returns>
        public static string GetData(string configDataModelStr) {
            ConfigDataModel model = JSSerialize.Deserialize<ConfigDataModel>(configDataModelStr);
            return GetData(model);
        }

        /// <summary>
        /// 根据配置条件查找ConfigData
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string GetData(ConfigDataModel model) 
        {
            if (model == null) { throw new Exception("ConfigDataModel参数未传"); }
            if (string.IsNullOrEmpty(model.Code)) {
                throw new Exception("ConfigDataMode.Code未传值");
            }
            if (string.IsNullOrEmpty(model.ConfigDataFile))
            {
                throw new Exception("ConfigDataMode.ConfigDataFile未传值");
            }
            string configDataFilePath = twiConfigDataPath + "/" + model.ConfigDataFile+".config";
            TwiPathHelper.ExistFile(configDataFilePath, true, "查找的ConfigData文件:"+model.ConfigDataFile+"不存在");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(configDataFilePath);
            XmlElement root = xmlDoc.DocumentElement;
            XmlNode node = default(XmlNode);
            string xpath = string.Format("/Root/Data[@Code='{0}']", model.Code);
            if (model.IsForChild) {
                xpath += string.Format("/{0}[@Code='{1}']",model.ChildName,model.ChildCode);
            }
            node = root.SelectSingleNode(xpath);

            if (node == null)
            {
                throw new Exception("ConfigData中未找到Code为：【" + model.Code + "】的配置");
            }
            return node.InnerText;
        }
    }

    /// <summary>
    /// ConfigData配置所需的参数实体
    /// </summary>
    public class ConfigDataModel
    {
        /// <summary>
        /// ConfigData文件名（只是名称，非路径+名称,同时不需要加后缀，后缀现在统一为.config）
        /// </summary>
        public string ConfigDataFile { get; set; }

        /// <summary>
        /// ConfigData唯一标识编号
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 子节点名称 如：<Data><Item Code="001"></Item></Data> 则为Item
        /// </summary>
        public string ChildName { get; set; }

        /// <summary>
        /// 子节点唯一标识编号
        /// </summary>
        public string ChildCode { get; set; }

        /// <summary>
        /// 是否查询子节点
        /// </summary>
        public bool IsForChild{
            get {
                if (string.IsNullOrEmpty(ChildCode) || string.IsNullOrEmpty(ChildName))
                {
                    return false;
                }
                else {
                    return true;
                }
            }
        }
    }
}
